package com.cs.usser.web;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
public class userAction {
	@RequestMapping("user")
	public String user(HttpServletRequest httpServletRequest){
		return "user:" + httpServletRequest.getServerPort();
	}

	//远程访问index 工程
	@Resource
	RestTemplate restTemplate;
	@RequestMapping("index")
	public String user(){
		String ret = restTemplate.getForObject(
				//用加载到eureka 注册的服务器名 替代 ip+端口
				"http://demo111/index",String.class);
		return "远程访问index:" + ret;
	}

	@RequestMapping("hello")
	public String hello(String name){
		return "hello" +name;
	}

	@RequestMapping("add")
	public int add(int a,int b){
		return a+b;
	}

	@RequestMapping("reg")
	public User reg(@RequestBody User user){	//调用服务也要加 不止接口加
		//对传递数据模型进行改变 返回改变的数据模型
		user.setName(user.getName() +"xxx");
		return user;
	}

	@RequestMapping("path/{ptest}")
	public String ptest(@PathVariable("ptest") String ptest){
		return ptest+"demo";
	}
}

//用户实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
class User {
	private String name;
	private Integer age;
	private Date birthday;
	private String[] likes;

}



